package org.ipmsg.core.file;

import java.io.*;
import java.net.Socket;
import org.ipmsg.core.Protocol;

public class TransferFile extends Thread {

    public TransferFile(Socket clientSocket, String filepath, long command) {
        this.clientSocket = clientSocket;
        this.filepath = filepath;
        this.command = command;
        start();
    }

    @Override
    public void run() {
        File file;
        try {
            responseInputStream = clientSocket.getOutputStream();
            file = new File(filepath);
            if (!file.exists()) {
                try {
                    clientSocket.close();
                } catch (IOException ioexception) {
                }
                return;
            }

            switch ((int) command) {
                case 96: // '`'
                    sendFile(file);
                    break;

                case 98: // 'b'
                    sendDir(file);
                    break;
            }
            responseInputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                clientSocket.close();
            } catch (IOException ioexception1) {
            }
        }
    }

    public void sendFile(File file)
            throws Exception {
        FileInputStream fileInputStream = new FileInputStream(file);
        long fileSize = file.length();
        int read = 0;
        long readCount = 0L;
        byte buffer[] = new byte[0x10000];
        while (readCount < fileSize) {
            read = fileInputStream.read(buffer, 0, (int) Math.min(fileSize - readCount, buffer.length));
            if (read < 0) {
                break;
            }
            readCount += read;
            responseInputStream.write(buffer, 0, read);
        }
        fileInputStream.close();
    }

    public void sendDir(File file)
            throws Exception {
        String fileName = (new StringBuilder(String.valueOf(file.getName()))).append(":").toString();
        String fileSize = (new StringBuilder(String.valueOf(Long.toHexString(file.length())))).append(":").toString();
        String fileModifie = (new StringBuilder(String.valueOf(Long.toHexString(Protocol.FILE_MTIME)))).append("=").append(Long.toHexString(file.lastModified() / 1000L)).append(":").toString();
        String fileCreate = (new StringBuilder(String.valueOf(Long.toHexString(Protocol.FILE_CREATETIME)))).append("=").append(Long.toHexString(file.lastModified() / 1000L)).append(":").toString();
        long opt = file.isHidden() ? Protocol.FILE_HIDDENOPT : 0L;
        opt |= file.canWrite() ? 0L : Protocol.FILE_RONLYOPT;
        if (file.isFile()) {
            String s1 = (new StringBuilder(String.valueOf(fileName))).append(fileSize).append(Long.toHexString(Protocol.FILE_REGULAR | opt)).append(":").append(fileModifie).append(fileCreate).toString();
            s1 = (new StringBuilder("00")).append(Long.toHexString(s1.getBytes("GBK").length + 5)).append(":").append(s1).toString();
            responseInputStream.write(s1.getBytes("GBK"));
            sendFile(file);
        } else {
            String s2 = (new StringBuilder(String.valueOf(fileName))).append(fileSize).append(Long.toHexString(Protocol.FILE_DIR | opt)).append(":").append(fileModifie).append(fileCreate).toString();
            s2 = (new StringBuilder("00")).append(Long.toHexString(s2.getBytes("GBK").length + 5)).append(":").append(s2).toString();
            responseInputStream.write(s2.getBytes("GBK"));
            File files[] = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                sendDir(files[i]);
            }
            String s3 = (new StringBuilder(".:")).append(fileSize).append(Long.toHexString(Protocol.FILE_RETPARENT | opt)).append(":").append(fileModifie).append(fileCreate).toString();
            s3 = (new StringBuilder("00")).append(Long.toHexString(s3.getBytes("GBK").length + 5)).append(":").append(s3).toString();
            responseInputStream.write(s3.getBytes("GBK"));
        }
    }
    private Socket clientSocket;
    private String filepath;
    private long command;
    private OutputStream responseInputStream;
}